package connectionSettings;

import java.util.Vector;

import javax.microedition.lcdui.Display;

import connections.Connection;
import connections.ConnectionListener;
import connections.HTTPDanoConnection;



public class HttpConnectionSettings extends ConnectionSettings  {
	
	
	ConnectionListener cl;

	
	String queryString = "?";
	
	String filename = "./up/";
	
	public HttpConnectionSettings(ConnectionListener _cl ){
		super("Http Terminal");
		cl = _cl;

	}
	public HttpConnectionSettings(Display _d ){
		super("Http Terminal", _d);
		cl = this;
	}
	
	public void displayProtocolOptions() {
		append("No additional preferences for http connections.");
	}

	
	public void displayConnectionInterface(){
		super.displayConnectionInterface();
		Vector methods = new Vector();
		methods.addElement("Connect");
		presentActionChoices("", methods);
		address.setLabel("http address <optional path and file>");
		if (getCurrentAddress() == null || getCurrentAddress().equals("null") || getCurrentAddress().equals("Empty")){
			address.setString("http://itp.nyu.edu/");
		}
	}
	public Connection getConnection(){
		return myConnection;
	}
	
	public void feedback(Connection _c, String _feedback) {
		if (cl != null) cl.feedback(_c, _feedback);
		super.feedback(_c, _feedback);
	}
	
	
	//public String getFilename(){
	//	return filename;
	//}
	
	public void madeChoice(int _which, String _choice) {

		if (_choice.equals("Connect")) {
			
			String[] params = getAddressParams();
			//if (params.length > 1) filename = params[1];
			
			String url = params[0];
			setPrefs();
			myConnection = new HTTPDanoConnection(url,cl);// this constructor does not trigger connection made
			
			//myConnection = new HTTPDanoConnection(url,cl,filename);// this constructor does not trigger connection made
			//myConnection.setFilename(filename );  //want to do this before connection made
			//super.connectionMade(myConnection,getCurrentAddress());  //notify so it will remember address
		
			//myConnection.addListener(cl);  //ongoing correspondence should also go there
			//cl.connectionMade(myConnection,url); //tell your papa
		
		} 
	}

	public void keyDown(int keyCode) {
		if (keyCode < 1){
			((HTTPDanoConnection)myConnection).doGet(getCurrentAddress() + queryString);
			queryString = "?";
		}else{
			queryString = queryString + keyCode;
		
		}
	}

	public void incoming(Connection _c, byte[] _input) {
		cl.incoming(_c,_input);
		
	}

}
